package net.hotelOrderSystem.service;

import net.hotelOrderSystem.domain.Hotel;
import net.hotelOrderSystem.domain.HotelExample;
import net.hotelOrderSystem.mapper.HotelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;

@Service
public class HotelServiceImpl implements HotelService{
    @Autowired
    HotelMapper hotelMapper;

    @Override
    public List<Hotel> findHotel() {

        return hotelMapper.selectByExample(new HotelExample());
    }

    //多关键词检索酒店
    @Override
    public List<Hotel> findHotelByKeyword(String allKeyword ) {
        String[] keywords=allKeyword.split(" ");
        String keyword;
        List<Hotel> hotels ;
        HotelExample hotel = new HotelExample();
        hotels = hotelMapper.selectByExample(hotel);
        hotels.clear();
        
        for(int i=0;i<keywords.length;i++) {
            keyword = keywords[i];
            hotel.createCriteria().andHotelIdLike("%" + keyword + "%");//匹配keyword
            hotels.addAll(hotelMapper.selectByExample(hotel));
            hotel.clear();
            hotel.createCriteria().andHotelTranslatedNameLike("%" + keyword + "%");//带有keyword的
            hotels.addAll(hotelMapper.selectByExample(hotel));
            hotel.clear();
            hotel.createCriteria().andStateLike("%" + keyword + "%");
            hotels.addAll(hotelMapper.selectByExample(hotel));
            hotel.clear();
            hotel.createCriteria().andCityLike("%" + keyword + "%");
            hotels.addAll(hotelMapper.selectByExample(hotel));
            hotel.clear();
            hotel.createCriteria().andAddressline1Like("%" + keyword + "%");
            hotels.addAll(hotelMapper.selectByExample(hotel));
            hotel.clear();
            hotel.createCriteria().andAddressline2Like("%" + keyword + "%");
            hotels.addAll(hotelMapper.selectByExample(hotel));
            hotel.clear();
            hotel.createCriteria().andBrandIdLike("%" + keyword + "%");
            hotels.addAll(hotelMapper.selectByExample(hotel));
            hotel.clear();
            hotel.createCriteria().andBrandNameLike("%" + keyword + "%");
            hotels.addAll(hotelMapper.selectByExample(hotel));
            hotel.clear();
            hotel.createCriteria().andChainIdLike("%" + keyword + "%");
            hotels.addAll(hotelMapper.selectByExample(hotel));
            hotel.clear();
            hotel.createCriteria().andChainNameLike("%" + keyword + "%");
            hotels.addAll(hotelMapper.selectByExample(hotel));
            hotel.clear();
            hotel.createCriteria().andCheckinLike("%" + keyword + "%");
            hotels.addAll(hotelMapper.selectByExample(hotel));
            hotel.clear();
            hotel.createCriteria().andCheckoutLike("%" + keyword + "%");
            hotels.addAll(hotelMapper.selectByExample(hotel));
            hotel.clear();
            hotel.createCriteria().andCityIdLike("%" + keyword + "%");
            hotels.addAll(hotelMapper.selectByExample(hotel));
            hotel.clear();
            hotel.createCriteria().andContinentIdLike("%" + keyword + "%");
            hotels.addAll(hotelMapper.selectByExample(hotel));
            hotel.clear();
            hotel.createCriteria().andContinentNameLike("%" + keyword + "%");
            hotels.addAll(hotelMapper.selectByExample(hotel));
            hotel.clear();
            hotel.createCriteria().andCountryIdLike("%" + keyword + "%");
            hotels.addAll(hotelMapper.selectByExample(hotel));
            hotel.clear();
            hotel.createCriteria().andCountryisocodeLike("%" + keyword + "%");
            hotels.addAll(hotelMapper.selectByExample(hotel));
            hotel.clear();
            hotel.createCriteria().andCountryLike("%" + keyword + "%");
            hotels.addAll(hotelMapper.selectByExample(hotel));
            hotel.clear();
            hotel.createCriteria().andHotelFormerlyNameLike("%" + keyword + "%");
            hotels.addAll(hotelMapper.selectByExample(hotel));
            hotel.clear();
            hotel.createCriteria().andHotelNameLike("%" + keyword + "%");
            hotels.addAll(hotelMapper.selectByExample(hotel));
            hotel.clear();
            hotel.createCriteria().andLatitudeLike("%" + keyword + "%");
            hotels.addAll(hotelMapper.selectByExample(hotel));
            hotel.clear();
            hotel.createCriteria().andLongitudeLike("%" + keyword + "%");
            hotels.addAll(hotelMapper.selectByExample(hotel));
            hotel.clear();
            hotel.createCriteria().andNumberfloorsLike("%" + keyword + "%");
            hotels.addAll(hotelMapper.selectByExample(hotel));
            hotel.clear();
            hotel.createCriteria().andNumberOfReviewsLike("%" + keyword + "%");
            hotels.addAll(hotelMapper.selectByExample(hotel));
            hotel.clear();
            hotel.createCriteria().andNumberroomsLike("%" + keyword + "%");
            hotels.addAll(hotelMapper.selectByExample(hotel));
            hotel.clear();
            hotel.createCriteria().andOverviewLike("%" + keyword + "%");
            hotels.addAll(hotelMapper.selectByExample(hotel));
            hotel.clear();
            hotel.createCriteria().andPhoto1Like("%" + keyword + "%");
            hotels.addAll(hotelMapper.selectByExample(hotel));
            hotel.clear();
            hotel.createCriteria().andPhoto2Like("%" + keyword + "%");
            hotels.addAll(hotelMapper.selectByExample(hotel));
            hotel.clear();
            hotel.createCriteria().andPhoto3Like("%" + keyword + "%");
            hotels.addAll(hotelMapper.selectByExample(hotel));
            hotel.clear();
            hotel.createCriteria().andPhoto4Like("%" + keyword + "%");
            hotels.addAll(hotelMapper.selectByExample(hotel));
            hotel.clear();
            hotel.createCriteria().andPhoto5Like("%" + keyword + "%");
            hotels.addAll(hotelMapper.selectByExample(hotel));
            hotel.clear();
            hotel.createCriteria().andRatesCurrencyLike("%" + keyword + "%");
            hotels.addAll(hotelMapper.selectByExample(hotel));
            hotel.clear();
            hotel.createCriteria().andRatesFromLike("%" + keyword + "%");
            hotels.addAll(hotelMapper.selectByExample(hotel));
            hotel.clear();
            hotel.createCriteria().andStateLike("%" + keyword + "%");
            hotels.addAll(hotelMapper.selectByExample(hotel));
            hotel.clear();
            hotel.createCriteria().andUrlLike("%" + keyword + "%");
            hotels.addAll(hotelMapper.selectByExample(hotel));
            hotel.clear();
            hotel.createCriteria().andYearopenedLike("%" + keyword + "%");
            hotels.addAll(hotelMapper.selectByExample(hotel));
            hotel.clear();
            hotel.createCriteria().andYearrenovatedLike("%" + keyword + "%");
            hotels.addAll(hotelMapper.selectByExample(hotel));
            hotel.clear();
            hotel.createCriteria().andZipcodeLike("%" + keyword + "%");
            hotels.addAll(hotelMapper.selectByExample(hotel));
            hotel.clear();
        }   
        HashSet h = new HashSet(hotels);
        hotels.clear();
        hotels.addAll(h);
        return hotels;
    }
//筛选检索
    @Override
    public List<Hotel> findHotelByFilter(String longitude, String latitude, Integer priceLow, Integer priceTop, double star, double rate, String brand) {
        List<Hotel> hotels ;
        HotelExample hotel = new HotelExample();
        HotelExample.Criteria criteria = hotel.createCriteria();
        if (longitude!="") criteria.andLongitudeLike(longitude.substring(0,4)+"%");
        if (latitude!="") criteria.andLatitudeLike(latitude.substring(0,4)+"%");
        if(priceLow!=null)  criteria.andPriceLowLessThan(priceLow);
        if(priceTop!=null)  criteria.andPriceTopGreaterThan(priceTop);
        if(star!=0)   criteria.andStarRatingGreaterThanOrEqualTo(star);
        if(rate!=0)   criteria.andRatingAverageGreaterThanOrEqualTo(rate);
        if(brand!=null) criteria.andBrandNameLike("%"+brand+"%");

        hotels=hotelMapper.selectByExample(hotel);

        return hotels;
    }

    @Override
    public void saveHotel(Hotel hotel) {
        if(hotel.getHotelId() == null) {
            hotelMapper.insert(hotel);
        } else {
            hotelMapper.updateByPrimaryKey(hotel);
        }
    }

    @Override
    public void deleteHotelByID(String hotelId) {
        HotelExample hotel = new HotelExample();
        hotel.createCriteria().andHotelIdEqualTo(hotelId);
        hotelMapper.deleteByExample(hotel);
    }

    @Override
    public void updateHotelByID(String hotelId, Hotel hotel) {
        HotelExample hotel1 = new HotelExample();
        hotel1.createCriteria().andHotelIdEqualTo(hotelId);
        hotelMapper.updateByExample(hotel,hotel1);
    }

    @Override
    public Hotel findHotelByID(String hotelId) {
        HotelExample hotel = new HotelExample();
        hotel.createCriteria().andHotelIdEqualTo(hotelId);
        return hotelMapper.selectByExample(hotel).get(0);
    }

    @Override
    public Hotel findHotelByLocation(String longitude, String latitude) {
        HotelExample hotelExample =new HotelExample();
        hotelExample.createCriteria().andLongitudeEqualTo(longitude).andLatitudeEqualTo(latitude);
        return hotelMapper.selectByExample(hotelExample).get(0);
    }

}
